<template>
	<view class="container">
		<u-sticky>
			<view class="header">
				<u-tabs
					:scrollable="false"
					:list="tabList"
					:activeStyle="{ color: '#E9930A' }"
					lineColor="#E9930A"
					@change="changeTab"
				/>
			</view>
		</u-sticky>
		<view class="courses-list flex-space-b flex-wrap">
			<course-column
				v-for="item in listData"
				:key="item.id"
				:item="item"
				:show-btn="false"
				:is-order="true"
			/>
		</view>
		<view class="empty-box">
			<u-empty v-if="loadData.status === 'nodata'" text="暫無數據" mode="data" icon="https://cdn.uviewui.com/uview/empty/data.png" />
			<u-loadmore
				v-else
				loadingIcon="circle"
				color="#aaa"
				:status="loadData.status"
				:loading-text="loadData.loadingText"
				:nomore-text="loadData.nomoreText"
			/>
		</view>
		<!-- 登录弹窗 -->
		<login-pop />
		<!-- 协议弹窗 -->
		<protocol-pop />
	</view>
</template>

<script>
	import CourseColumn from '@/components/Course/column'
	import { queryBtOrder } from '@/api/user'
	export default {
		components: {
			CourseColumn
		},
		data() {
			return {
				tabList: [{
					name: '已開課程',
					value: 0
				}, {
					name: '未開課程',
					value: 1
				}, {
					name: '已結束課程',
					value: 2
				}],
				listParams: {
					status: 0, // 0 已開課程 1未開課程 2已結束課程
					sort: 'sorder',
					sortType: 'desc',
					pageNum: 1,
					pageSize: 10
				},
				listData: [],
				loadData: {
					status: 'loading',
					loadingText: '努力加載中',
					nomoreText: '沒有更多了'
				}
			}
		},
		async onLoad() {
			this.initPage()
		},
		methods: {
			async getList() {
				const { pageNum, pageSize } = this.listParams
				const { data } = await queryBtOrder(this.listParams, { needLoading: false })
				data.result.map(v => {
					v.imghome = v.courseimg
					v.name = v.courseName
					v.signupId = v.id
					v.id = v.courseId
				})
				if (data.result.length === 0 && pageNum === 1) {
					this.loadData.status = 'nodata'
					this.listData = []
					return
				}
				if (data.result.length < pageSize) {
					this.loadData.status = 'nomore'
				}
				this.listParams.pageNum++
				this.listData = this.listData.concat(data.result)
			},
			async initPage() {
				this.listData = []
				Object.assign(this.listParams, {
					pageNum: 1
				})
				this.loadData.status = 'loading'
				await this.getList()
			},
			changeTab(e) {
				this.listParams.status = e.value
				this.initPage()
			}
		},
		onReachBottom() {
			if (this.loadData.status === 'loading') {
				this.getList()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background: #fff;
		padding-bottom: 20rpx;
		box-sizing: border-box;
	}
	.header {
		/deep/ .u-tabs__wrapper__nav__item {
			flex: 1 1 auto;
		}
	}
	.courses-list {
		padding: 20rpx;
	}
	/deep/ .empty-box .u-empty {
		height: 70vh;
	}
</style>